<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="主键" prop="id" v-if="false">
				<el-input v-model="dataForm.id" placeholder="主键"></el-input>
			</el-form-item>

			<el-row>
				<el-col :span="12">
					<el-form-item label="员工主键" prop="staffId">
						<el-input v-model="dataForm.staffId" placeholder="员工主键"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="离职原因" prop="resignationReason">
						<el-input v-model="dataForm.resignationReason" placeholder="离职原因"></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="公司名称" prop="companyName">
						<el-input v-model="dataForm.companyName" placeholder="公司名称"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="职位" prop="jobs">
						<el-input v-model="dataForm.jobs" placeholder="职位"></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="入职日期" prop="startDate">
						<ElDatePicker v-model="dataForm.startDate" placeholder="请选择" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="离职日期" prop="endDate">
						<ElDatePicker v-model="dataForm.endDate" placeholder="请选择" />
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="工作时长" prop="workHours">
						<el-row>
							<el-col :span="16">
								<el-input v-model="dataForm.workHours" placeholder="工作时长"></el-input>
							</el-col>
							<el-col :span="8">
								<el-tree-select
									v-model="dataForm.workHoursTypeId"
									:data="gongZuoShiChang"
									value-key="id"
									check-strictly
									clearable
									:render-after-expand="false"
									:props="{ value: 'id', label: 'name', children: 'children' }"
									style="width: 100%"
								/>
							</el-col>
						</el-row>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="离职工资" prop="salary">
						<el-input v-model="dataForm.salary" placeholder="离职工资">
							<template #append>元</template>
						</el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-form-item label="岗位职责" prop="jobDuties">
				<el-input v-model="dataForm.jobDuties" placeholder="岗位职责"></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="description">
				<el-input v-model="dataForm.description" placeholder="备注"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useExperienceApi, useExperienceSubmitApi } from '@/api/nb/staff/experience'
import { dictByCode } from '@/hooks/hot'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	creator: '',
	createTime: '',
	updater: '',
	updateTime: '',
	version: '',
	deleted: '',
	staffId: '',
	resignationReason: '',
	companyName: '',
	jobs: '',
	startDate: '',
	endDate: '',
	workHours: '',
	workHoursTypeId: '',
	salary: '',
	jobDuties: '',
	description: ''
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getExperience(id)
	}

	dictByCode('GongZuoShiChang', gongZuoShiChang)
}

const gongZuoShiChang = ref()

const getExperience = (id: number) => {
	useExperienceApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useExperienceSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
